import React, { useState } from 'react';
import { View, Text, TextInput, TouchableOpacity, StyleSheet, Image, SafeAreaView, StatusBar, Alert } from 'react-native';
import { useAuth } from '../navigation/AppNavigator';

export default function Login() {
  const { setIsLoggedIn } = useAuth();
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');
  const [showPassword, setShowPassword] = useState(false);

  const handleLogin = () => {
    if (!username.trim() || !password.trim()) {
      Alert.alert('提示', '请输入账号和密码');
      return;
    }
    
    // 这里可以添加实际的登录验证逻辑
    // 目前简单设置为登录成功
    setIsLoggedIn(true);
  };

  return (
    <SafeAreaView style={styles.container}>
      <StatusBar barStyle="light-content" backgroundColor="#7B83FF" />
      {/* 顶部插画占位 */}
      <View style={styles.illustrationBox}>
        {/* 可替换为本地图片 */}
        <Image
          source={{ uri: 'https://img.icons8.com/clouds/200/000000/iphone-x.png' }}
          style={styles.illustration}
          resizeMode="contain"
        />
      </View>
      {/* 标题 */}
      <Text style={styles.title}>一站式移动办公平台</Text>
      {/* 输入框区域 */}
      <View style={styles.formBox}>
        <TextInput
          style={styles.input}
          placeholder="请输入账号"
          placeholderTextColor="#C5CAE9"
          value={username}
          onChangeText={setUsername}
        />
        <View style={styles.passwordRow}>
          <TextInput
            style={[styles.input, { flex: 1, marginBottom: 0 }]}
            placeholder="请输入密码"
            placeholderTextColor="#C5CAE9"
            secureTextEntry={!showPassword}
            value={password}
            onChangeText={setPassword}
          />
          {/* 密码可见图标 */}
          <TouchableOpacity 
            style={styles.eyeIcon}
            onPress={() => setShowPassword(!showPassword)}
          >
            <Text style={{ color: '#B0B3C6', fontSize: 18 }}>
              {showPassword ? '🙈' : '👁️'}
            </Text>
          </TouchableOpacity>
        </View>
        <TouchableOpacity style={styles.loginBtn} onPress={handleLogin}>
          <Text style={styles.loginBtnText}>登录</Text>
        </TouchableOpacity>
      </View>
      {/* 协议提示 */}
      <Text style={styles.protocolText}>
        登录代表你同意
        <Text style={styles.link}>《用户协议》</Text>
        和
        <Text style={styles.link}>《隐私政策》</Text>
      </Text>
      {/* 忘记密码 */}
      <TouchableOpacity style={styles.forgotBox}>
        <Text style={styles.forgotText}>忘记密码</Text>
      </TouchableOpacity>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#7B83FF',
    alignItems: 'center',
  },
  illustrationBox: {
    width: '100%',
    alignItems: 'center',
    marginTop: 40,
    marginBottom: 10,
  },
  illustration: {
    width: 180,
    height: 120,
  },
  title: {
    color: '#fff',
    fontSize: 26,
    fontWeight: 'bold',
    marginBottom: 30,
    marginTop: 10,
    alignSelf: 'flex-start',
    marginLeft: 30,
  },
  formBox: {
    width: '88%',
    backgroundColor: '#fff',
    borderRadius: 16,
    padding: 24,
    shadowColor: '#000',
    shadowOpacity: 0.06,
    shadowRadius: 8,
    elevation: 4,
    marginBottom: 18,
  },
  input: {
    height: 44,
    backgroundColor: '#F5F6FA',
    borderRadius: 8,
    paddingHorizontal: 16,
    fontSize: 16,
    color: '#333',
    marginBottom: 16,
  },
  passwordRow: {
    flexDirection: 'row',
    alignItems: 'center',
    marginBottom: 16,
  },
  eyeIcon: {
    marginLeft: -36,
    padding: 8,
  },
  loginBtn: {
    backgroundColor: '#7B83FF',
    borderRadius: 24,
    height: 48,
    alignItems: 'center',
    justifyContent: 'center',
    marginTop: 8,
  },
  loginBtnText: {
    color: '#fff',
    fontSize: 18,
    fontWeight: 'bold',
    letterSpacing: 2,
  },
  protocolText: {
    color: '#E0E0E0',
    fontSize: 13,
    marginTop: 18,
    textAlign: 'center',
  },
  link: {
    color: '#333',
    fontWeight: 'bold',
  },
  forgotBox: {
    position: 'absolute',
    bottom: 40,
    width: '100%',
    alignItems: 'center',
  },
  forgotText: {
    color: '#7B83FF',
    fontSize: 15,
    textDecorationLine: 'underline',
  },
});
